package com.woniuxy.mesboot.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.mesboot.util.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ：lisir
 * @date ：Created in 2023/5/30 10:49
 * @description：TODO
 */
public class LoginAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        String value = new ObjectMapper().writeValueAsString(Result.error("用户名或密码错误"));
        response.getWriter().println(value);
    }
}
